<template>
  <div class="dialog">
    <el-dialog :title="'区域' + titleText" :visible.sync="showDialog" width="35%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="区域名称：" class="w100p" prop="areaName">
          <el-input v-model="formData.areaName" clearable placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="区域类型：" class="w100p" prop="areaType">
          <el-select filterable clearable v-model="formData.areaType" clearable filterable placeholder="选择区域类型">
            <el-option v-for="(item, index) of areaTypeList" :key="index + 'area'" :label="item.value"
              :value="item.name" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="备注：" class="w100p" prop="description">
          <el-input
            v-model="formData.description"
            clearable
            :rows="2"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AreaApi from "@/api/basic-info-manage/area";
import EnumApi from "@/api/enum";
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      areaTypeList: [],
      rules: {
        areaName: [
          { required: true, message: "请输入区域名称", trigger: ["change", "blur"] }
        ],
        areaType: [
          { required: true, message: "选择区域类型", trigger: ["change", "blur"] }
        ]
      },
      showDialog: this.show

    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryEnum()
  },
  methods: {
    queryEnum() {
      EnumApi.list({ enumType: "AreaType" }).then(res => {
        this.areaTypeList = res.data
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            AreaApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            AreaApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
